import { ProjectConfig } from '@/config'

const NODE_ENV = process.env.NODE_ENV

export function getEnvName() {
  return process.env.NODE_ENV
}

let currentEnv = ''

export function setEnvironment(env: string) {
  currentEnv = env
  console.log('--setEnvironment---', env)
  sessionStorage.setItem(ProjectConfig.ENV_STORAGE_NAME, env)
}

export function getEnv(): string {
  if (currentEnv) {
    return currentEnv
  }
  const storageEnv = sessionStorage.getItem(ProjectConfig.ENV_STORAGE_NAME)
  if (storageEnv) {
    currentEnv = storageEnv
  } else {
    currentEnv = getNativePlatform()
  }
  return currentEnv
}

export function getNativePlatform(): string {
  return webIsIos() ? 'ios' : 'android'
}

export function webIsIos(): boolean {
  const u = navigator.userAgent
  return u.indexOf('iPhone') > -1 || u.indexOf('iphone') > -1
}

export function isTest(): boolean {
  return NODE_ENV === 'test'
}

export function isProd(): boolean {
  return NODE_ENV === 'production'
}

export function isDev(): boolean {
  return NODE_ENV === 'development'
}
